package com.app;


import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.app.net.NetHelper;
import com.app.umeng.Umeng;
import com.app.util.Utils_Log;

import java.util.HashMap;
import java.util.Map;


public abstract class BaseActivity extends  FragmentActivity {
	private Toast mToast;
	public RequestQueue requestQueue;
	public RequestQueue mRequestQueue;
	protected String TAG = getClass().getSimpleName();
	protected FragmentActivity activity = this;
	
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		Utils_Log.d("activity_life", getClass().getName() + "----->create");

		Map<String,Object> SystemBarData = new HashMap<>();
		boolean bUse = getSystemBarColor(SystemBarData);
		Integer color = (Integer) SystemBarData.get("DiyColor");
		Boolean bUseDiyColor = (Boolean) SystemBarData.get("bUseDiyColor");
		if(color == null)
			color = Color.TRANSPARENT;
		if(bUseDiyColor == null)
			bUseDiyColor = false;

		if(bUse){
			G.initSystemBar(this, bUseDiyColor, color);
		}
		Utils_Log.d("initSystemBar", "bUse:"+bUse+" bUseDiyColor "+bUseDiyColor + " color "+color);

		Umeng.OnCreate_analytics(this);
		Umeng.OnCreate_message(this);
		
		this.requestQueue = Volley.newRequestQueue(this);
		this.mRequestQueue = this.requestQueue;

	
		AppManager.getAppManager().addActivity(this);

		Object obj = InitLayout();
		if(obj instanceof View){
			setContentView((View)obj);
		}
		else{
			setContentView( (int)obj );
		}
		InitView();
		InitListener();
		InitProcess();
		
		
	}
	
	// 注册广播接收器关闭对话框
	


	/**
	 * 根据输入文字提示信息
	 * 
	 * @param text
	 *            提示信息
	 */
	public void ShowToast(final String text) {
		if (!TextUtils.isEmpty(text)) {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {

					if (mToast == null) {
						mToast = Toast.makeText(getApplicationContext(), text,
								Toast.LENGTH_SHORT);
					} else {
						mToast.setText(text);
					}
					mToast.show();
				}
			});

		}
	}

	/**
	 * 根据ID提示信息
	 * 
	 * @param resId
	 *            资源ID
	 */
	public void ShowToast(final int resId) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if (mToast == null) {
					mToast = Toast.makeText(
							BaseActivity.this.getApplicationContext(), resId,
							Toast.LENGTH_SHORT);
				} else {
					mToast.setText(resId);
				}
				mToast.show();
			}
		});

	}
	
	
	
	private long lastClickTime;

	/**
	 * 判断事件出发时间间隔是否超过预定值
	 * 
	 * @Description
	 * @return
	 * @Author lewis(lgs@yitong.com.cn) 2014-4-29 上午9:12:20
	 */
	public boolean isFastDoubleClick() {
		long time = System.currentTimeMillis();
		long timeD = time - lastClickTime;
		if (0 < timeD && timeD < 1000) {
			return true;
		}
		lastClickTime = time;
		return false;
	}

	
	
	
	
	// -------------------------------------隐藏输入法-----------------------------------------------------
    // 获取点击事件
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = { 0, 0 };
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
    
    
	
	
	///////////////////////////////////////////////////////////////////////////////////////
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Utils_Log.d("activity_life", getClass().getName() + "----->start");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Utils_Log.d("activity_life", getClass().getName() + "----->restart");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Utils_Log.d("activity_life", getClass().getName() + "----->resume");
		
		Umeng.OnResume_analytics(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Utils_Log.d("activity_life", getClass().getName() + "----->pause");
		
		Umeng.OnPause_analytics(this);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Utils_Log.d("activity_life", getClass().getName() + "----->stop");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		
		Umeng.onDestroy(this);
		
		if (this.requestQueue != null){
			this.requestQueue.cancelAll( NetHelper.TAG_URL );
		}
		
		AppManager.getAppManager().finishActivity(this);
		Utils_Log.d("activity_life", getClass().getName() + "----->destroy");
	}
	
	
	
	public abstract Object InitLayout();
	public abstract void InitView();
	public abstract void InitListener();
	public abstract void InitProcess();


	public boolean getSystemBarColor(Map<String,Object> data){
		return true;
	}


	
}



